﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Services;
using System.Reflection;
using ADONETDataServicesExtension.Utils;
using System.Collections;

namespace ADONETDataServicesExtension
{
    public class RequestDescription : FrameworkEncapsulatedType<object>
    {
        internal const string FrameworkTypeName = "System.Data.Services.RequestDescription";

        public RequestDescription(object requestDescription)
            : base(requestDescription)
        {
        }

        public RequestDescription(int targetKind, int targetSource, Uri resultUri)
        {
            FrameworkObject = ReflectionHelper.CreateInstance(FrameworkTypeName, ReflectionHelper.GetFrameworkType(RequestTargetKind.FrameworkTypeName).GetEnumValueFromInt(targetKind), ReflectionHelper.GetFrameworkType(RequestTargetSource.FrameworkTypeName).GetEnumValueFromInt(targetSource), resultUri);
        }
        public RequestDescription(SegmentInfo[] segmentInfos, string containerName, bool usesContainerName, string mimeType, Uri resultUri)
        {
            int segmentInfosLength = segmentInfos.Length;
            var frameworkSegmentInfos = Array.CreateInstance(ReflectionHelper.GetFrameworkType(SegmentInfo.FrameworkTypeName), segmentInfosLength);
            for (int i = 0; i < segmentInfosLength; i++)
                frameworkSegmentInfos.SetValue(segmentInfos[i].FrameworkObject, i);
            FrameworkObject = ReflectionHelper.CreateInstance(FrameworkTypeName, frameworkSegmentInfos, containerName, usesContainerName, mimeType, resultUri);
        }
        public RequestDescription(RequestDescription other, IEnumerable queryResults, List<ExpandSegmentCollection> expandPaths)
        {
            FrameworkObject = ReflectionHelper.CreateInstance(FrameworkTypeName, other.FrameworkObject, queryResults, expandPaths);
        }

        public int TargetKind
        {
            get
            {
                return ReflectionHelper.GetProperty<int>(FrameworkTypeName, "TargetKind", FrameworkObject);
            }
        }

        public IEnumerable RequestEnumerable
        {
            get
            {
                return ReflectionHelper.GetProperty<IEnumerable>(FrameworkTypeName, "RequestEnumerable", FrameworkObject);
            }
            set
            {
                ReflectionHelper.SetProperty(FrameworkTypeName, "RequestEnumerable", FrameworkObject, value);
            }
        }

        public bool IsSingleResult
        {
            get
            {
                return ReflectionHelper.GetProperty<bool>(FrameworkTypeName, "IsSingleResult", FrameworkObject);
            }
        }

        public int TargetSource
        {
            get
            {
                return ReflectionHelper.GetProperty<int>(FrameworkTypeName, "TargetSource", FrameworkObject);
            }
        }

        public Type TargetElementType
        {
            get
            {
                return ReflectionHelper.GetProperty<Type>(FrameworkTypeName, "TargetElementType", FrameworkObject);
            }
        }

        public SegmentInfo[] SegmentInfos
        {
            get
            {
                return ReflectionHelper.GetProperty<IEnumerable>(FrameworkTypeName, "SegmentInfos", FrameworkObject).Cast<object>().Select(si => new SegmentInfo(si)).ToArray();
            }
        }

        public List<ExpandSegmentCollection> ExpandPaths
        {
            get
            {
                return ReflectionHelper.GetProperty<List<ExpandSegmentCollection>>(FrameworkTypeName, "ExpandPaths", FrameworkObject);
            }
        }

        public bool LinkUri
        {
            get
            {
                return ReflectionHelper.GetProperty<bool>(FrameworkTypeName, "LinkUri", FrameworkObject);
            }
        }

        public string MimeType
        {
            get
            {
                return ReflectionHelper.GetProperty<string>(FrameworkTypeName, "MimeType", FrameworkObject);
            }
        }
    }
}
